package com.mayuanfei.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.aizuda.snailjob.client.core.annotation.Propagation;
import com.aizuda.snailjob.client.core.annotation.Retryable;
import com.aizuda.snailjob.client.core.retryer.RetryType;
import com.mayuanfei.common.constant.SceneConstant;
import com.mayuanfei.common.exception.BizException;
import com.mayuanfei.service.LocalRetryService;
import org.springframework.stereotype.Service;

/**
 * TODO 类描述
 *
 * @author 老马
 * @date 2025-04-07 11:16
 */
@Service
public class LocalRetryServiceImpl implements LocalRetryService {
    @Override
    @Retryable(scene = SceneConstant.LOCAL_RETRY, retryStrategy = RetryType.ONLY_LOCAL, localTimes = 2, localInterval = 3)
    public void localRetry(String params) {
        System.out.println("local retry 方法开始执行");
        ThreadUtil.sleep(4000);
        throw new RuntimeException("local retry 方法执行异常");
    }

    @Override
    public void localRetryWithAnnotation(String params) {
        System.out.println("local retry 方法开始执行");
        throw new RuntimeException("local retry 方法执行异常");
    }

    @Override
    @Retryable(scene = SceneConstant.LOCAL_RETRY + "2"
            , retryStrategy = RetryType.ONLY_LOCAL
    )
    public void localRetryNoException(String params) {
        System.out.println("测试异常方法开始执行");
        throw new BizException("10001", "测试异常方法执行异常");
    }

    @Retryable(scene = SceneConstant.LOCAL_RETRY + "3"
            , retryStrategy = RetryType.ONLY_LOCAL
            , retryMethod = MyRetryMethod.class)
    @Override
    public void localRetryMyMethod(String params) {
        System.out.println("local retry my method 方法开始执行");
        throw new RuntimeException("local retry my method 方法执行异常");
    }

    @Retryable(scene = SceneConstant.LOCAL_RETRY + "4"
            , retryStrategy = RetryType.ONLY_LOCAL
            , propagation = Propagation.REQUIRED)
    @Override
    public void localPropagation(String params) {
        System.out.println("local retry propagation 方法开始执行");
        throw new RuntimeException("local retry propagation 方法执行异常");
    }

    @Retryable(scene = SceneConstant.LOCAL_RETRY + "5",
            retryStrategy = RetryType.ONLY_LOCAL,
            include = {BizException.class})
    @Override
    public void localRetryIncludeException(String params) {
        System.out.println("local retry include exception 方法开始执行");
        throw new RuntimeException("local retry include exception 方法执行异常");
    }

    @Retryable(scene = SceneConstant.LOCAL_RETRY + "6",
            retryStrategy = RetryType.ONLY_LOCAL,
            exclude = {BizException.class})
    @Override
    public void localRetryExcludeException(String params) {
        System.out.println("local retry exclude exception 方法开始执行");
        throw new BizException("10001", "local retry exclude exception 方法执行异常");
    }
    @Retryable(scene = "localRetryPropagation",
            retryStrategy = RetryType.ONLY_LOCAL)
    @Override
    public void localRetryPropagation(String params) {
        System.out.println("local retry propagation 方法开始执行");
        // 调用A方法
        LocalRetryServiceImpl agentThisClass = SpringUtil.getBean(LocalRetryServiceImpl.class);
        agentThisClass.localRetryPropagationA(params);
    }

    @Retryable(scene = "localRetryPropagationA",
            retryStrategy = RetryType.ONLY_LOCAL,
            propagation = Propagation.REQUIRES_NEW)
    public void localRetryPropagationA(String params) {
        System.out.println("local retry propagation A");
        LocalRetryServiceImpl agentThisClass = SpringUtil.getBean(LocalRetryServiceImpl.class);
        // 调用B方法
        agentThisClass.localRetryPropagationB(params);
    }

    @Retryable(scene = "localRetryPropagationB",
            retryStrategy = RetryType.ONLY_LOCAL,
            propagation = Propagation.REQUIRED
            , isThrowException = false
            )
    public void localRetryPropagationB(String params) {
        System.out.println("local retry propagation B");
        throw new RuntimeException("local retry propagation B");
    }

    @Retryable(scene = "retryCompleteCallback",
            retryStrategy = RetryType.ONLY_LOCAL,
            retryCompleteCallback = MyRetryCompleteCallback.class)
    @Override
    public void localRetryCompleteCallback(String params) {
        System.out.println("local retry complete callback 方法开始执行");
        throw new RuntimeException("local retry complete callback 方法执行异常");
    }


}